<template>
  <div class="container">
  

    <InfoDialog :dialogVisable="infoDialog" :id="params.businessKey"  :out="out" :flag="flag" @changeDialog="changeDialog"

    :procInsId='params.procInsId'  @changeSuccess="changeSuccess" @changerRefuse="changerRefuse" :unitNameList="unitNameList"></InfoDialog>

  </div>

</template>
<script>
import {
getDelect,
getDetailById,
getDictionary,
getExport,
getLock,
getTable,
getUnit,
getUnlock,
startProcess
} from "@/views/expert/api/expertsManager";

import Table from "@/views/science/components/table/table.vue";
import { Modals, tips } from "@/views/science/components/tips/tips";
import { ElAlert } from 'element-plus';
import { nextTick, onMounted, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import InfoDialog from "./InfoDialog.vue";
// import InfoDialog from "@/views/expert/pool/expertsManager/InfoDialog.vue";
import tableHead from "./tableHeader.json";

export default {

  props: {
    params: {
      type: Object,
      default: {},
    },
    out:{
      type: String,
      default:'0',
    }
  },

  components: {
    Table,
    InfoDialog,
    ElAlert
  },
  setup(props) {
    const store = useStore();

    // 发送请求
    onMounted(() => {
      // 获取数据字典
      getCodeList();
      // 获取单位
      getUnitList();
      // 列表信息table
      getTableList();
      handlesh({id:props.params.businessKey})
    });

    let data = reactive({
      status: 0, //0编辑，1审核
      dealVisible: false,
      times: null,
      id: "",
      flag: 1,
      infoDialog: false,
      ids: [],
      pagination: { page: 1, pageSize: 10, total: 0 }, //分页
      isLoading: false,

      sexList: [],
      areaList: [],
      unitNameList: [],
      awardList: [],
      tableDate: [],
      expertTypeList: [{ itemValue: 1, itemText: '公司内部人才' }, { itemValue: 2, itemText: '公司外部人才' }],

      columns: tableHead.softwareHeader,
      search: {
        type: "2",
        expertType: '',
        expertName: "",
        genderId: "",
        department: "",
        job: "",
        expertiseAreaId: "",
      },
    });


    let handletj = (row) => {

      Modals("是否确认当前流程", "提示", "warning")
        .then(async () => {

          let res = await startProcess({

            flowKey: 'talents',
            businessKey: row.id,
            businessType: 'YW05',
            random: (new Date()).valueOf()
          });

          if (res.data.code == 200) {

            data.dealVisible = true

            deal(row.id, 1)

          } else {
            
            tips(res.data.message, 'warning')
          }

        })
        .catch(() => { });

    }

    let deal = (id, state) => {


      data.times = setInterval(async () => {

        getDetailById(id).then((res) => {

          const resData = res.data.data

          if (resData.status == state) {

            data.dealVisible = false


            nextTick(() => {

              data.infoDialog = false

              getTableList()

              clearTimeout(data.times)

            })

          }

        })
      }, 1000);




    }

    // 解锁
    let getUnlockBtn = async () => {
      if (data.ids.length == 1) {
        Modals("是否解锁选择内容", "提示", "warning")
          .then(async () => {
            let res = await getUnlock(data.ids);
            if (res.data.success) {
              tips("解锁成功", "success");
              getTableList();
            }
          })
          .catch(() => {
            console.log("点击了取消");
          });
      } else if (data.ids.length == 0) {
        tips("请选择", "warning");
      } else {
        tips("解锁功能不可批量操作", "warning");
      }
    };
    //  锁定
    let getLockBtn = async () => {
      if (data.ids.length == 1) {
        Modals("是否锁定选择内容", "提示", "warning")
          .then(async () => {
            let res = await getLock(data.ids);
            if (res.data.success) {
              tips("锁定成功", "success");
              getTableList();
            }
          })
          .catch(() => {
            console.log("点击了取消");
          });
      } else if (data.ids.length == 0) {
        tips("请选择", "warning");
      } else {
        tips("锁定功能不可批量操作", "warning");
      }
    };

    // 导出功能的实现

    let getExportBtn = () => {
      let msg = '是否导出全部数据'
      if (data.ids.length > 0) {
        msg = '是否导出所选数据'
      }
      Modals(msg, "提示", "warning")
        .then(async () => {
          let obj = {
            ...data.search,
            ids: data.ids,
          }
          await getExport(obj, "人才库");
        })
        .catch(() => {
          console.log("点击了取消");
        });
    };


    // 批量删除
    const handleDetect = async () => {
      if (data.ids.length) {
        Modals("是否删除选择内容", "提示", "warning").then(async () => {
          let list = []
          data.tableDate.forEach(e => {
            if (data.ids.includes(e.id)) {
              list.push(e.status)
            }
          })
          if (list.includes('办理中') || list.includes('正常办结') || list.includes('已退回') || list.includes('已撤办') || list.includes('强制办结') || list.includes('已废弃') || list.includes('已挂起')) {
            tips('只允许删除待提交和已驳回状态的数据', 'warning')
          } else {
            let res = await getDelect(data.ids);
            if (res.data.success) {
              tips("删除成功", "success");
              getTableList();
            }
          }
        })
          .catch(() => {
            console.log("点击了取消");
          });
      } else {
        tips("请选择", "warning");
      }
    };

    // 获取列表信息
    let getTableList = () => {

      data.isLoading = true,

        getTable({
          current: data.pagination.page,
          size: data.pagination.pageSize,
          ...data.search,
        }).then((res) => {


          let resData = res.data.data // 获取列表数据
          resData.records.map((item) => {
            if (item.status == 0) {
              item.status = "待提交"
            } else if (item.status == 1) {
              item.status = "办理中"
            } else if (item.status == 2) {
              item.status = "正常办结"
            } else if (item.status == 3) {
              item.status = "已退回"
            }
            else if (item.status == 4) {
              item.status = "已撤办"
            }
            else if (item.status == 5) {
              item.status = "强制办结"
            }
            else if (item.status == 6) {
              item.status = "已废弃"
            }
            else if (item.status == 7) {
              item.status = "已挂起"
            }
            else if (item.status == 8) {
              item.status = "已驳回"
            }
          })
          data.tableDate = resData.records;
          data.pagination.total = res.data.data.total;
          data.isLoading = false;
        });
    };

    // 分页

    let currentChange = (current) => {

      data.pagination.page = current;

      getTableList();

    };

    // 获取ids

    let delectIds = (ids) => {
      data.ids = ids;
    };

    // 单位
    let getUnitList = () => {
      getUnit({}).then((res) => {
        data.unitNameList = res.data.data;
      });
    };

    //  用户列表
    let getCodeList = () => {
      getDictionary(["SEX", 'ZJYRC_SCLY']).then((res) => {
        data.sexList = res.data.data.SEX;
        data.areaList = res.data.data.ZJYRC_SCLY;
      });
    };

    let changerRefuse = () => {

      data.dealVisible = true

      deal(data.id, 8)

    }

    let changeSuccess = () => {

      data.dealVisible = true


      data.times = setInterval(async () => {

        getDetailById(data.id).then((res) => {

          const resData = res.data.data

          if (resData.status == 2 || resData.status == 1 && resData.currTaskTd !== data.taskId) {

            data.dealVisible = false

            nextTick(() => {

              data.infoDialog = false

              getTableList()

              clearTimeout(data.times)

            })

          }

        })
      }, 1000);

    }



    // 新增
    let handleAdd = () => {
      data.flag = 1;

      data.infoDialog = true;
    };
    // 编辑

    let handleEdit = (row) => {
      data.flag = 2;
      data.id = row.id;
      data.infoDialog = true;
    };

    // 审核按钮
    const handlesh = (row) => {
      data.flag = 3;
      data.id = row.id;
      data.infoDialog = true;
    }

    let changeDialog = (value) => {
      data.id = "";
      data.flag = 1;

      if (value) {
        data.pagination.page = 1;
        getTableList();
      }

      // 分页刷新列表
      data.infoDialog = false;
    };

    let submit = () => {

      tips("该功能正在开发中.....", "warning");

    }

    return {
      ...toRefs(data),
      changeSuccess,
      submit,
      handlesh,
      store,
      handleEdit,
      handleAdd,
      changeDialog,
      getTableList,
      getLockBtn,
      getExportBtn,
      getUnlockBtn,
      delectIds,
      handleDetect,
      currentChange,
      handletj,
      changerRefuse
    };
  },
};
</script>
<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
}
</style>
